package com.techfort.metalrss;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.techfort.metalrss.core.Feed;
import com.techfort.metalrss.utils.WebUtil;

public class ListChannels extends Activity {

	private static String METALRSS_SERVER = "http://www.dsforge.net/metalrss/server.php?action=select&lang=";
	private TextView title;
	private Gson gson = new Gson();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Bundle extras = this.getIntent().getExtras();
		int language = extras.getInt("LANG");
		String languageName = extras.getString("LANGUAGE");
		title = (TextView) findViewById(R.id.title);
		title.setText(languageName + " channels:");
		
		//String [] channels = getResources().getStringArray(getLanguageArrayResource(language));
		String json;
		try {
			Type feedList = new TypeToken< List<Feed> >() {
			}.getType();
			json = WebUtil.fetch(METALRSS_SERVER + language);
			List<Feed> feeds = (List<Feed>) gson.fromJson(json, feedList);
			
			ListView list = (ListView) findViewById(R.id.mainlist);
			list.setAdapter(new ChannelAdapter(this, R.layout.rssitem, feeds));
		} catch (IOException e) {
			Log.e("ERROR", e.getMessage());
		}
		
		
	}
	
	
	private class ChannelAdapter extends ArrayAdapter<Feed>{

		List<Feed> items;
		
		public ChannelAdapter(Context context, int textViewResourceId,
				List<Feed> objects) {
			super(context, textViewResourceId, objects);
				items = objects;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if(v==null){
				LayoutInflater li = (LayoutInflater) ListChannels.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = li.inflate(R.layout.rssitem, null);
			}
			final Feed feed = items.get(position);
			final String link = items.get(position).getLink(); 
			final String feedName = items.get(position).getFeed_name();
			
			TextView tv = (TextView) v.findViewById(R.id.title);
			tv.setText(feedName);
			v.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					Intent intent = new Intent(ListChannels.this, ListFeeds.class);
					Bundle extras = new Bundle();
					extras.putString("RSSURL",link);
					extras.putParcelable("FEED", feed);
					intent.putExtras(extras);
					startActivity(intent);
					
				}
			});
			return v;
		}
		
	}
	

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	
    	MenuInflater mi = new MenuInflater(this);
    	mi.inflate(R.menu.menu, menu);
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch(item.getItemId()){
    	case R.id.favourites:
    		Intent intent = new Intent(ListChannels.this, Favourites.class);
    		startActivity(intent);
    	case R.id.home:
    		Intent home = new Intent(ListChannels.this, Main.class);
    		startActivity(home);
    	default:
    		return true;
    	}
    }
}
